const {
  $util,
} = getApp();
const app = getApp();
Page({
  data: {
    istrue: true,
    ischeck: true,
    openContact: false,
    isShowLoading: true, //是否显示loading
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
    bgImg: $util.getImgUrl(),
  },
  onLoad() {
    this.wechatworker();
  },
  wechatworker() {
    let that = this;
    app.util.request({
      url: 'common/wechatworker/geCsInfo',
      data: {},
      showLoading: false,
      success: function (res) {
        res.data.data && res.data.data.status && that.setData({
          plugid: res.data.data.plugid,
        });
      },
      fail: function (err) {
        console.log('fail', err);
      },
    });
  },
  hidePop() {
    this.setData({
      openContact: false,
    });
  },
  showPop() {
    this.setData({
      openContact: true,
    });
  },
  startmessage: function (e) {
    // body...
  },
  completemessage: function (e) {
    // -3006，当前已是好友，提示退出小程序进行对话。个人不再受到[服务通知]推送
    if (e.detail.errcode === -3006) {
      return wx.showModal({
        title: '提示',
        content: '您已添加该客服为您的微信好友，您可以退出小程序并从您的[通讯录]-[企业微信联系人]中找到该客服进行对话。',
      });
    }
    if (e.detail.errcode === -3005) {
      return wx.showModal({
        title: '提示',
        content: '客服消息发送失败',
      });
    }
    if (e.detail.errcode === -3008) {
      return wx.showModal({
        title: '提示',
        content: '当前配置没有客服人员',
      });
    }
    if (e.detail.errcode === 0) {
      wx.showModal({
        title: '提示',
        content: '消息已发送成功',
      });
    }
  },
  onShow() {
    this.loadUserInfo();
  },
  navigateTo(e) {
    const {
      url,
      title = '',
    } = e.currentTarget.dataset;
    let urlArr = url.split('?');
    let paramArr = {};
    paramArr.barTitle = title;
    if (urlArr.length > 1) {
      let param = urlArr[1].split('&');
      for (let i = 0; i < param.length; i++) {
        paramArr[param[i].split('=')[0]] = param[i].split('=')[1];
      }
    }
    app.util.navigateTo(urlArr[0], paramArr);
  },
  loadMenuList() {
    $util.request({
      url: 'entry/wxapp/base.def.apps',
      data: {
        type: 'mine', // 'dynamic','study','work','mine'
        version: 1,
      },
      showLoading: false,
      success: (res) => {
        this.setData({
          menus: res.apps,
          header: res.header || [],
        });
      },
    });
  },
  loadUserInfo() {
    $util.request({
      url: 'entry/wxapp/partyman.mine.index',
      data: {},
      showLoading: this.data.isShowLoading,
      success: (res) => {
        this.loadMenuList();
        this.getButlins();
        this.setData({
          ...res,
          isShowLoading: false,
          isSaaS: res.isThirdApp,
        });
      },
    });
  },
  // 更新用户信息
  getInfo(e) {
    $util.updateUser(e.detail, () => {
      this.loadUserInfo();
      if (this.data.avatar) {
        wx.showToast({
          title: '更新用户信息成功',
          icon: 'none',
        });
      }
    });
  },
  // 认证
  getauth(e) {
    $util.updateUser(e.detail, () => {
      wx.navigateTo({
        url: '/ws_party_const/pages/auth/index',
      });
    });
  },
  getButlins() {
    app.util.request({
      url: 'auth/session/builtins',
      showLoading: false,
      success: (res) => {
        if (res.data.errno === 0) {
          this.setData({
            builtins: res.data.data,
          });
        }
      },
    });
  },
  goLogout() {
    $util.request({
      url: 'entry/wxapp/user.authentication.logout',
      data: {},
      showLoading: false,
      success: (res) => {
        wx.showToast({
          title: '退出成功',
          mask: true,
          success: () => {
            setTimeout(() => {
              wx.reLaunch({
                url: res.isrz ? '/ws_party_const/pages/login/login' : '/ws_party_const/pages/auth/index',
              });
            }, 1000);
          },
        });
      },
    });
  },
  onShareAppMessage() {
    return {
      path: '/ws_party_const/pages/user/user',
    };
  },
  onPullDownRefresh() {
    wx.stopPullDownRefresh();
    this.loadUserInfo();
  },
});